(function(){
    var $modal = $('#pop-modal');
    //图片上传
    $('.portrait-box').find('img')

     //获取层级
    laodAreas ({});
    getUser();
    function getUser(){
        getJson(Domain+'/mbr/doLoadUserInfo.do',{mbrId:329},function (res) {
            if(!res.success){
                    $('.am-modal-bd').text(res.errorMessage);
                    $modal.modal();
                    setTimeout(function () {
                    $modal.modal('close');
                    },iWiating);

            }else{
                userHtml(res.data.userInfo);
            }
        })
    }
   /* 获得用户的值渲染页面*/
    function userHtml(userInfo){
        $('#preview').attr('src',userInfo.imgUrl);
        $('.name').text(userInfo.name);
        $('.mobile').text(userInfo.mobile);
        $('input[name="nickName"]').val(userInfo.nickName);


        if (userInfo.sex!=0) {
            if (userInfo.sex==1) {
                $('input[name="sex"][value="1"]').attr("checked", true);
            }else if(userInfo.sex==2){
                $('input[name="sex"][value="2"]').attr("checked", true);
            }
        }
        $('input[name="email"]').val(userInfo.email);

        $("#provincelist").find('option[value="105879"]').attr("selected",true);

        $('input[name="displayName"]').val(userInfo.displayName);
        $('input[name="mbrIdCode"]').val(userInfo.mbrIdCode);
        $('input[name="bankCode"]').val(userInfo.bankCode);
    }

   
     function laodAreas (Filter)
     {
        getJson(Domain+'/common/doLoadAllArea.do', Filter, function (res) {

          if(!res.success){
            $('.am-modal-bd').text(res.errorMessage);
                $modal.modal();
                setTimeout(function () {
                    $modal.modal('close');
                },iWiating);

          }
          else{
             console.log(res);
            for (var i = 0; i< res.data.length; i++)
            {
                $('#provincelist').append('<option value="'+res.data[i].value+'">'+res.data[i].text+'</option>');
            }

            oMenuData = res.data;


          }
        },function (error) {

            $('.am-modal-bd').text(language.ajaxFailed);
                            $modal.modal();
                        setTimeout(function () {
                            $modal.modal('close');
                        },iWiating);

        });
     }

    //生成对应城市层级
    function createCityList(index,data)
    {
        $('#citylist').empty().append('<option value="" selected>市</option>');

            for (var i = 0 ; i < data[index].children.length; i++)
            {
                $('#citylist').append('<option value="'+data[index].children[i].value+'">'+data[index].children[i].text+'</option>');
            }

            createAreaList(data[index].children[0]);
    }

    //生成对应区域数据
    function createAreaList(data)
    {
        $('#arealist').empty().append('<option value="" selected>县</option>');

        for (var i = 0 ; i < data.children.length; i++)
        {
            $('#arealist').append('<option value="'+data.children[i].value+'">'+data.children[i].text+'</option>');
        }
    }


    //省份市级联动
    $('#provincelist').on('change',function () {

        iProvinceIndex = $(this).find('option').index($(this).find('option:selected'));
        iProvinceIndex = parseInt(iProvinceIndex)-1;

        if(iProvinceIndex==-1)
        {
            $('#citylist').empty().append('<option value="" selected>市</option>');
            $('#arealist').empty().append('<option value="" selected>县</option>');
            return;
        }
        createCityList(iProvinceIndex,oMenuData);

    });

    //市级区域联动
    $('#citylist').on('change',function () {

        iCityIndex = $(this).find('option').index($(this).find('option:selected'));
        iCityIndex = parseInt(iCityIndex)-1;

        if(iCityIndex==-1)
        {
            $('#arealist').empty().append('<option value="" selected>县</option>');
            return;
        }

        createAreaList(oMenuData[iProvinceIndex].children[iCityIndex]);

    });

    //区域查询
    $('#arealist').on('change',function () {

        iAreaIndex = $(this).find('option').index($(this).find('option:selected'));
        iAreaIndex = parseInt(iAreaIndex)-1;

        if(iAreaIndex==-1)
        {
            return;
        }

    });

    /*头像上传*/
    $('#doc').change(function(){
        setImagePreview();
    })
      //下面用于图片上传预览功能
    function setImagePreview(avalue) {
        var docObj=document.getElementById("doc");
        var imgObjPreview=document.getElementById("preview");
        if(docObj.files &&docObj.files[0]){
            imgObjPreview.style.display = 'block';
            imgObjPreview.style.width = '132px';
            imgObjPreview.style.height = '132px'; 


            imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
        }else{
            docObj.select();
            var imgSrc = document.selection.createRange().text;
            var localImagId = document.getElementById("localImag");
            localImagId.style.width = "150px";
            localImagId.style.height = "180px";
            try{
            localImagId.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
            localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
            }
            catch(e)
            {
            alert("您上传的图片格式不正确，请重新选择!");
            return false;
            }
            imgObjPreview.style.display = 'none';
            document.selection.empty();
        }
        return true;
    }
  
})();